/*
 * Created by JFormDesigner on Wed Dec 23 19:34:33 CST 2020
 */

package cn.fourhundredandeighteen.view.TeacherView.teacherCourseInfo;

import cn.fourhundredandeighteen.controller.CourseController;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;
import cn.fourhundredandeighteen.view.StudentView.StudentCourseInfo.studentCourseInfo;
import cn.fourhundredandeighteen.view.TeacherView.teacherCourseInfo.addCourse.addcourse;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

/**
 * @author root
 */
public class teacherCourseInfo extends JPanel {
    private final techercourse techercourse = new techercourse();
    private JScrollPane techercourse1;
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label2;
    private JTextField textField1;
    private JButton button2;
    private JButton button1;
    private JButton button3;

    public teacherCourseInfo() {
        initComponents();
        techercourse1 = techercourse.techercourse();
        this.add(techercourse1);
        this.updateUI();

    }

    private void getData() {
        this.remove(techercourse1);
        techercourse1 = techercourse.techercourse();
        this.add(techercourse1);
        this.updateUI();

    }

    // 添加课程按钮
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        JFrame jFrame = new JFrame();
        addcourse addcourse = new addcourse();
        jFrame.setBounds(600, 250, 600, 500);
        jFrame.add(addcourse);
        jFrame.setVisible(true);
        getData();
        Container.getInstance().upDatePage();
    }

    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
        CourseController courseController = new CourseController();
        try {
            Result result = courseController.delCourse(Long.valueOf(studentCourseInfo.newText));
            if (result.getMassage() == "SUCCESS") {
                JOptionPane.showMessageDialog(null, "删除成功");
                // 更新页面
                getData();
                Container.getInstance().getRight().updateUI();
            } else {
                JOptionPane.showMessageDialog(null, "请输入正确课程号");
            }
        } catch (NumberFormatException err) {
            JOptionPane.showMessageDialog(null, "请输入课程号");
        }


    }

    private void button3ActionPerformed(ActionEvent e) {
        // TODO add your code here
        JFrame jFrame = new JFrame();
        updataCourse updataCourse = new updataCourse();
        jFrame.setBounds(600, 250, 600, 500);
        jFrame.add(updataCourse);
        jFrame.setVisible(true);
        getData();
        Container.getInstance().upDatePage();
    }

    private void thisFocusGained(FocusEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label2 = new JLabel();
        textField1 = new JTextField();
        button2 = new Button(Button.CANCELBUTTON);
        button1 = new Button();
        button3 = new Button();

        //======== this ========
        addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                thisFocusGained(e);
            }
        });
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 26, 15));

            //---- label2 ----
            label2.setText("\u8bf7\u8f93\u5165\u8bfe\u7a0b\u53f7");
            label2.setIconTextGap(5);
            panel1.add(label2);

            //---- textField1 ----
            textField1.setColumns(7);
            panel1.add(textField1);

            //---- button2 ----
            button2.setText("\u5220\u9664\u8bfe\u7a0b");
            button2.addActionListener(e -> button2ActionPerformed(e));
            panel1.add(button2);

            //---- button1 ----
            button1.setText("\u6dfb\u52a0\u8bfe\u7a0b");
            button1.addActionListener(e -> button1ActionPerformed(e));
            panel1.add(button1);

            //---- button3 ----
            button3.setText("\u8bfe\u7a0b\u4fe1\u606f\u66f4\u65b0");
            button3.addActionListener(e -> button3ActionPerformed(e));
            panel1.add(button3);
        }
        add(panel1, BorderLayout.SOUTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    public JTextField getTextField1() {
        return textField1;
    }
}
